package com.cff.action;

import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.text.JTextComponent;

import com.cff.common.Constant;
import com.cff.qq.TestInput;
import com.cff.util.RSAUtil;
import com.cff.util.SignTools;
import com.ums.common.hsm.HSMException;

public class RSAEnOrDecryptActionListener implements ActionListener {

	private String KeyType;
	
	private JTextComponent srcText;
	
	private JTextComponent desText;
	
	public RSAEnOrDecryptActionListener(JTextComponent srcText, JTextComponent desText, String KeyType) {
		super();
		this.srcText = srcText;
		this.desText = desText;
		this.KeyType = KeyType;
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		String key = "";
		String result = "";
		String text = srcText.getText();
		SignTools signTools = new SignTools();
		try {
			if("PUBLICKEY".equals(KeyType)){
				key = Constant.acctPublicKey;
				result = signTools.publicEncrypt(text, key);
			}
			if("PUBLICPROCKEY".equals(KeyType)){
				key = Constant.acctPublicProcKey;
				result = signTools.publicEncrypt(text, key);
			}
			if("PRIVATEKEY".equals(KeyType)){
				key = Constant.acctPrivateKey;
				result = signTools.privateDecrypt(text,key);
			}
			if(Constant.RSAPUBLICKEY.equals(KeyType)){
				key = Constant.RSAPublicKey;
				RSAPublicKey publicKey= RSAUtil.getPublicKey(key); 
				byte []resultByte = RSAUtil.encrypt(text,publicKey);
				result = RSAUtil.encryptBASE64(resultByte);
			}
			if(Constant.RSAPUBLICKEYQQ.equals(KeyType)){
				key = Constant.RSAPublicKey;
				RSAPublicKey publicKey= RSAUtil.getPublicKey(key); 
				byte []resultByte = RSAUtil.encrypt(text,publicKey);
				result = RSAUtil.encryptBASE64(resultByte);
				Robot robot = new Robot();
		        robot.delay(2000);
		        //输入字符串
		        TestInput.keyPressString(robot,result);
			}
			if(Constant.RSAPRIVATEKEY.equals(KeyType)){
				key = Constant.RSAPrivateKey;
				RSAPrivateKey privateKey= RSAUtil.getPrivateKey(key);
				result = RSAUtil.decryptString(RSAUtil.decryptBASE64(text),privateKey);
			}
			
		} catch (InvalidKeyException e1) {
			e1.printStackTrace();
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (NoSuchPaddingException e1) {
			e1.printStackTrace();
		} catch (IllegalBlockSizeException e1) {
			e1.printStackTrace();
		} catch (BadPaddingException e1) {
			e1.printStackTrace();
		} catch (NumberFormatException e1) {
			e1.printStackTrace();
		} catch (HSMException e1) {
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}	
		
		desText.setText(result);
	}

}
